package SOEN_Phase_3;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

public class TaskTest extends TestCase {
	
	public void testTask()
	{
		Task tsk = new Task(1, 15, "Title", "Person", 5, 10, true, new ArrayList<Task>(), new ArrayList<Integer>());
		
		Assert.assertNotSame(0, tsk.getID());
		Assert.assertNotSame(0, tsk.getDuration());
		Assert.assertNotSame("", tsk.getTitle());
		Assert.assertNotSame("", tsk.getAssignee());
		Assert.assertNotSame(0, tsk.getAssigneeID());
		Assert.assertNotSame(0, tsk.getTimeInvested());
		Assert.assertEquals(true, tsk.getCompletion());
		Assert.assertNotNull(tsk.getSubtasks());
	}
	
	public void testSetParentOnSubtasks()
	{
		Task tsk = new Task(1, 15, "Title", "Person", 5, 10, true, new ArrayList<Task>(), new ArrayList<Integer>());
		Task sub = new Task(5, 5, "Sub Title", "Person 2", 5, 0, false, new ArrayList<Task>(), new ArrayList<Integer>());
		tsk.addSubtask(sub);
		
		Assert.assertEquals(tsk.getID(), sub.getParent().getID());
	}

	public Document loadXMLFromString(String xml) throws Exception
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();

		return builder.parse(new ByteArrayInputStream(xml.getBytes()));
	}
	Document docc ;


	protected void setUp() throws Exception {

		super.setUp();
	}

	public void testGetTaskFromXMLNode() throws ParserConfigurationException {
		Task T;

		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		// Use factory to generate a new document builder:
		DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();

		// Create a Document object by parsing the XML document:
		try {
			docc = builder.parse (new File("tasks.xml"));

			//Node taskContents = doc.getChildNodes().item(0).getChildNodes().item(0);
			//String name = taskContents.getNodeValue();
			NodeList taskList = docc.getElementsByTagName("task");

			// Get a task item:
			Node task = taskList.item(0);
			T= Task.getTaskFromXMLNode(task);
			Assert.assertNotNull(T);
		} catch (SAXException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


	}

	public void testToXML() throws ParserConfigurationException {
		ArrayList<Task> subz = new ArrayList<Task>();
		ArrayList<Integer> depends = new ArrayList<Integer>();
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		// Use factory to generate a new document builder:
		DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();

		// Create a Document object by parsing the XML document:
		try {
			docc = builder.parse (new File("tasks.xml"));
			Node taskElement = null;
			int ID;
			Task tassk = new Task ( 99,5 ,"hello"," zzz",44,5,true, subz, depends);
			taskElement =tassk.toXML( docc,  false);
			NamedNodeMap att = taskElement.getAttributes();
			ID = Integer.parseInt( att.getNamedItem("ID").getNodeValue());

			Assert.assertEquals(99, ID);



		} catch (SAXException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}

	public void testRemoveSubtask() {
		ArrayList<Task> subz2 = new ArrayList<Task>();
		ArrayList<Task> subz = new ArrayList<Task>();
		ArrayList<Integer> depends = new ArrayList<Integer>();
	
		Task subtask = new Task(100, 10, "Title", "Izz", 7, 0, false, subz2, new ArrayList<Integer>());

		Task tassk = new Task ( 99,5 ,"hello"," zzz",44,5,true, subz, depends);	
		tassk.addSubtask(subtask);
		tassk.removeSubtask(100);
		int size = tassk.getSubtasks().size();
		Assert.assertEquals(0, size);
	
	}

	public void testRemoveFromParent() {

		ArrayList<Task> subz = new ArrayList<Task>();
		ArrayList<Integer> depends = new ArrayList<Integer>();

		Task tassk = new Task ( 99,5 ,"hello"," zzz",44,5,true, subz, depends);	
		Task subtask = new Task(100, 10, "Title", "Izz", 7, 0, false, new ArrayList<Task>(), new ArrayList<Integer>());
		tassk.addSubtask(subtask);
		Task SubtaskRef = tassk.getSubtasks().get(0);
		
		int sizeBeforeRemove = tassk.getSubtasks().size();
		SubtaskRef.removeFromParent();
		int sizeAfterRemove = tassk.getSubtasks().size();
		//Both of them should be 0 since Sizebeforeremove -1 = SizeAfterRemove
		Assert.assertEquals(sizeBeforeRemove -1, sizeAfterRemove);
		
		
		
		
	}

}
